﻿using System;

namespace ServiceLayerManagement
{
    public static class TimeHelper
    {
        /// <summary>
        /// DateTime转ulong(不精确到毫秒)
        /// </summary>
        public static ulong DateTimeToUlongTime(this DateTime dateTime)
        {
            TimeSpan ts = dateTime - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            return Convert.ToUInt64(ts.TotalSeconds);
        }
        /// <summary>
        /// ulong转DateTime（不精确到毫秒）
        /// </summary>
        public static DateTime UlongTimeToDateTime(this ulong time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当地时区
            return startTime.AddSeconds(time);
        }
        /// <summary>
        /// DateTime转ulong(精确到毫秒)
        /// </summary>
        public static ulong DateTimeToUlongMillisecond(this DateTime dateTime)
        {
            TimeSpan ts = dateTime - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToUInt64(ts.TotalMilliseconds);
        }
        /// <summary>
        /// ulong转DateTime（精确到毫秒）
        /// </summary>
        public static DateTime UlongMillisecondToDateTime(this ulong time)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));//当地时区  
            return startTime.AddMilliseconds(time);
        }
    }
}
